package com.universest.swordholder.ui.activity

import android.content.Intent
import android.os.Bundle
import com.universest.swordholder.R
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.universest.swordholder.ui.theme.SwordHolderTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.imePadding
import androidx.compose.material3.Button
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.lifecycleScope
import com.universest.swordholder.data.dao.Book
import com.universest.swordholder.data.dao.LongText
import com.universest.swordholder.data.dao.Passage
import com.universest.swordholder.data.getUserDb
import com.universest.swordholder.data.repository.BookRepository
import com.universest.swordholder.data.repository.LongTextRepository
import com.universest.swordholder.data.repository.PassageRepository
import com.universest.swordholder.model.user.UserManager
import com.universest.swordholder.tools.lengthWithoutSpace
import com.universest.swordholder.tools.normallyToBoolean
import com.universest.swordholder.ui.screen.MainScreen
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch

class MainActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        setTheme(R.style.Theme_SwordHolder)
        super.onCreate(savedInstanceState)
        lifecycleScope.launch {
            val configManager = UserManager.instance.getUserConfigManager()
            val firstInstall = configManager.getConfig("firstInstall","true").first()
            if(firstInstall?.normallyToBoolean() != false){
                configManager.saveConfig("firstInstall","false")
                doWhenFirstInstall()
            }
        }
    }

    @Composable
    override fun ActivityContentScreen(modifier: Modifier) {
        MainScreen(modifier = modifier.imePadding())
    }

    /**
     * 当应用首次安装时执行的初始化操作
     * 该函数会在数据库中创建默认的故事内容，包括书籍、段落和文本内容
     *
     * @return 无返回值
     */
    suspend fun doWhenFirstInstall(){
        // 获取用户数据库实例
        val db = getUserDb()
        val bookRepository = BookRepository(db)
        val passageRepository = PassageRepository(db)
        val longTextRepository = LongTextRepository(db)

        // 创建默认书籍并获取书籍ID
        val bookId = bookRepository.addBook(Book(
            bookName = getString(R.string.default_story_name),
            singlePassage = true,
            bookDescription = getString(R.string.default_story_description),
            charCount = getString(R.string.default_story_content).lengthWithoutSpace))

        // 插入默认的长文本内容并获取文本ID
        val longTextId = longTextRepository.insert(LongText(
            content = getString(R.string.default_story_content)
        ))

        // 创建段落关联书籍和文本内容
        passageRepository.insert(Passage(
            bookId = bookId.toInt(),
            contentId = longTextId.toInt(),
            title = getString(R.string.default_story_name),
        ))
    }
}
